package tianwang.biz.stat;

import java.util.List;

import tianwang.model.AticleModel;
import tianwang.model.CALexical;
import tianwang.services.collect.CollectResult;
import tianwang.services.collect.Collecter;

/**
 * 计算文章的平均词长
 * 
 * 总词长/单词数
 * 
 * 单纯的类
 */
public class WordsAvgLengthCollecter implements Collecter<CollectResult> {
	private final String name = CALexical.AVERAGE_WORD_LENGTH;
	
	public CollectResult collect(Object material) throws Exception {
		AticleModel model = (AticleModel) material;
		List<String> list = model.getEntityWords();
		
		if(list == null){
			return null;
		}
		
		
		float totalLen = 0;
		float count = 0;
		String entry;
		for(int i = 0, len = list.size(); i < len; i++){
			entry = list.get(i);
			
			totalLen += entry.length();
			count ++;
		}
		
		final float avgLength = totalLen/count;
		
		return new CollectResult(){
			public Object pump(){
				return (int)avgLength;
			}

			@Override
			public String getName() {
				return name;
			}
		};
	}
	


}
